﻿using Management.Auxiliary;
using Management.Core.Permission;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Management.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class PermissionController : Controller
    {
        private PermissionManager permissionManager = new PermissionManager();
        //
        // GET: /Control/Permission/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult PageList( int? pageNumber, int? pageSize, string controllerName, string actionName,int? order)
        {
            Paging<Permission> _pagingPermission = new Paging<Permission>();
            if (pageNumber != null && pageNumber > 0) _pagingPermission.PageIndex = (int)pageNumber + 1;
            if (pageSize != null && pageSize > 0) _pagingPermission.PageSize = (int)pageSize;
            var _paging = permissionManager.FindPageList(_pagingPermission, controllerName, actionName, null);
            return Json(new { total = _paging.TotalNumber, rows = _paging.Items });
        }

        /// <summary>
        /// 修改权限信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Modify(int id)
        {            
            return PartialView(permissionManager.Find(id));
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Modify(int id, FormCollection form)
        {
            Response _resp = new Response();
            var _permission = permissionManager.Find(id);
            _permission.Updated_at = DateTime.Now;
            try
            {
                if (TryUpdateModel(_permission, new string[] { "ControllerName", "ActionName", "ControllerNo", "ActionNo", "Updated_at" }))
                {
                    _resp = permissionManager.Update(_permission);
                    RedisHelper.DelKey("permissionList");//删除redis缓存                    
                }
                else
                {
                    _resp.Code = 0;
                    _resp.Message = "模型不符合条件，更新失败";
                }
            }
            catch (Exception ex)
            {
                _resp.Code = 0;
                _resp.Message = ex.Message;
            }
            return Json(_resp);
        }
	}
}